﻿using AuthCenter.Domain;
using AuthCenter.ErorCode;
using AuthCenter.Infrastructure;
using AuthCenter_API.Write.Applications.Commands.RBAC;
using MediatR;

namespace AuthCenter_API.Write.Applications.CommandHandlers.RBAC
{
    public class FenpeiAddCommandHandler : IRequestHandler<FenpeiAddCommand, APIResult<object>>
    {
        private readonly IBaseRepository<UserRoleModel> _urRepository;

        public FenpeiAddCommandHandler(IBaseRepository<UserRoleModel> urRepository)
        {
            _urRepository = urRepository;
        }

        public async Task<APIResult<object>> Handle(FenpeiAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = APIEnum.AddSuccess;
            result.Message = APIEnum.AddSuccess.Desc();
            List<int> userArr=request.UidList.Split(',').Select(x=>Convert.ToInt32(x)).ToList();
            foreach (var item in userArr)
            {
                UserRoleModel role = new()
                {
                    UserId=item,
                    RoleId=request.RoleId
                };
                await _urRepository.Add(role);
            }
            return await Task.FromResult(result);
        }
    }
}
